package main

import (
	"context"
	"fmt"
	"gitee.com/ha666/golibs"
	"github.com/astaxie/beego/logs"
	"github.com/smallnest/rpcx/client"
	"log"
	"rpc_models"
	"time"
)

var (
	addr    = "localhost:9999"
	d       = client.NewPeer2PeerDiscovery("tcp@"+addr, "")
	xclient client.XClient
)

func init() {
	option := client.DefaultOption
	option.Heartbeat = true
	option.HeartbeatInterval = 10 * time.Second
	xclient = client.NewXClient("BaseRPC", client.Failfast, client.RoundRobin, d, client.DefaultOption)
}

func main() {
	defer func() {
		if err := recover(); err != nil {
			logs.Error("【main】ex:%v", err)
			return
		}
	}()
	for i := 0; i < 100; i++ {
		{
			args := &rpc_models.SystemGetTimeArgs{
				Base: rpc_models.BaseRequest{
					Ver:    180201,
					AppKey: 2018020116,
					Time:   golibs.Unix(),
					V:      1,
					Sign:   fmt.Sprintf("%x", golibs.Sha1([]byte(`fmt.Println(dog.name + " eat")`))),
				},
			}
			reply := &rpc_models.SystemGetTimeReply{}
			err := xclient.Call(context.Background(), rpc_models.System_GetTime, args, reply)
			if err != nil {
				log.Printf("%s\t出错:%s\n", golibs.StandardTime(), err.Error())
			} else {
				if !reply.IsSuccess {
					log.Printf("%s\t出错:%d,%s\n", golibs.StandardTime(), reply.Code, reply.Msg)
				} else {
					log.Printf("result:%s", reply.CurrentTime.Format(golibs.Time_TIMEStandard))
				}
			}
		}
		time.Sleep(200 * time.Millisecond)
		{
			args := &rpc_models.ToolGetPwdArgs{
				Base: rpc_models.BaseRequest{
					Ver:    180201,
					AppKey: 2018020116,
					Time:   golibs.Unix(),
					V:      1,
					Sign:   fmt.Sprintf("%x", golibs.Sha1([]byte(`fmt.Println(dog.name + " eat")`))),
				},
				Length: i,
			}
			reply := &rpc_models.ToolGetPwdReply{}
			err := xclient.Call(context.Background(), rpc_models.Tool_GetPwd, args, reply)
			if err != nil {
				log.Printf("%s\t出错:%s\n", golibs.StandardTime(), err.Error())
			} else {
				if !reply.IsSuccess {
					log.Printf("%s\t出错:%d,%s\n", golibs.StandardTime(), reply.Code, reply.Msg)
				} else {
					log.Printf("result:%s", reply.Pwd)
				}
			}
		}
		time.Sleep(time.Second)
	}
}
